<#import "/common.ftl" as common />
<@common.commomFrame >
	<div id="breadcrumb">
		<ul class="breadcrumb">
			 <li><i class="fa fa-home"></i><a href="index-2.html">首页</a></li>
			 <li class="active">机车列表</li>
		</ul>
	</div>
	
	<div class="padding-md" >
		<div class="panel panel-default table-responsive">
			<div class="panel-body">
				<a href="#addForm" class="btn btn-info btn-xs" data-toggle="modal" ><i class="fa fa-plus fa-lg"></i> 新增</a>
			</div>
			
			<div class="panel-heading">条件搜索</div>
			<div class="panel-body">
				<form id="searchForm" class="form-inline no-margin" action="/locomotive/list" method="post">
					<div class="form-group" style="margin-right:10px;">
						<label class="control-label">机车名称</label>
						<div>
							<input name="locoName" type="text" class="form-control input-sm" value="${(locomotive.locoName)!}"/>
						</div>
					</div>
					
					<button type="submit" class="btn btn-sm btn-success"><i class="fa fa-search" style="font-size:16px;"></i></button>
				</form>
			</div>
			<table class="table table-bordered table-condensed table-hover table-striped" id="responsiveTable">
				<thead>
					<tr>
						<th>机车编号</th>
						<th>机车名称</th>
						<th>编组数</th>
						<th>编组类型</th>
						<th>定编数</th>
						<th>担当局</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<#if locoList?? && locoList?size gt 0>
						<#list locoList as loco>
						<tr>
							<td>${loco.locoId}</td>
							<td>${loco.locoName}</td>
							<td>${loco.marshallingNumber}</td>
							<td>
								<#if (loco.marshallingType)?? && loco.marshallingType==1>
									<span class="label label-success">长编</span>
								<#elseif (loco.marshallingType)?? && loco.marshallingType==2>
									<span class="label label-danger">短遍</span>
								<#else>
									<span class="label label-danger">混合遍</span>
								</#if>
							 </td>
							<td>${loco.setNum}</td>
							<td>${(loco.bearBureauName)!}</td>
							<td>
								<a class="btn btn-xs btn-success" data-toggle="modal" href="#editForm" onclick="getLoco('${loco.locoId}')"><i class="fa fa-wrench fa-lg"></i> 编辑</a>
							</td>
						</tr>
						</#list>
					</#if>
				</tbody>
			</table>
		</div>
	</div>
	
	<#-- 新增机车信息 -->
	<div class="modal fade" id="addForm">
		<div class="modal-dialog">
			<div class="modal-content">
  				<div class="modal-header">
    				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4>新增机车</h4>
  				</div>
			    <div class="modal-body">
					<form id="addLocoForm" class="form-horizontal form-border" method="post" action="/user/save">
						<div class="form-group">
							<label class="col-md-2 control-label text-right">机车名称</label>
							<div class="col-md-7 ">
								<input  type="text" id="locoName1" name="locoName"  placeholder="请输入机车名称" class="form-control input-sm"/>
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label text-right">编组数</label>
							<div class="col-md-7 ">
								<input  type="text" id="marshallingNumber1"  name="marshallingNumber" placeholder="请输入编组数" class="form-control input-sm"" />
							</div>
						</div>
						<div class="form-group">
							 <label class="col-md-2 control-label"><span class="text-danger">*</span>编组类型</label>
							<div class="col-md-10">
								<label class="label-radio inline">
									<input type="radio" name="marshallingType" value="1" required="required" 
									  data-parsley-errors-container="#roleError" checked>
									<span class="custom-radio"></span>
									长编
								</label>
								<label class="label-radio inline">
									<input type="radio" name="marshallingType" value="2" required="required" data-parsley-errors-container="#roleError" >
									<span class="custom-radio"></span>
									短遍
								</label>
								<label class="label-radio inline">
									<input type="radio" name="marshallingType" value="2" required="required" data-parsley-errors-container="#roleError" >
									<span class="custom-radio"></span>
									混合编
								</label>
							</div>
							<span id="roleError"></span>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label text-right">定编数</label>
							<div class="col-md-7 ">
								<input  type="text" id="setNum1"  name="setNum" placeholder="请输入定编数" class="form-control input-sm"" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label"><span style="color: red">*</span>担当局</label>
							<div class="col-md-3">
								<select name="bearBureau" id="bearBureau1" class="form-control chzn-select"  data-placeholder="请选择担当局">
									
									<#if departmentList?? && departmentList?size gt 0>
										<#list departmentList as department>
											<option value="${(department.id)!}">${(department.departName)!}</option>
										</#list>
									</#if>
								</select>
							</div>
						</div>
						
						<div class="form-group text-center">
							<button type="button" class="btn btn-success" onclick="addLoco()"> 保存  </button>
							<button type="button" class="btn btn-default" data-dismiss="modal"> 关闭  </button>
						</div>
					</form>
			    </div>
		  	</div>
		</div>
	</div>
	
	<#-- 编辑机车信息 -->
	<div class="modal fade" id="editForm">
  		<div class="modal-dialog">
			<div class="modal-content">
  				<div class="modal-header">
    				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4>编辑机车</h4>
  				</div>
			    <div class="modal-body">
					<form id="editLocoForm" class="form-horizontal form-border" method="post" action="/user/save">
						<div class="form-group">
							<label class="col-md-2 control-label text-right">机车名称</label>
							<div class="col-md-7 ">
								<input  type="text" id="locoName2" name="locoName"  placeholder="请输入机车名称" class="form-control input-sm"/>
								<input type="hidden" id="locoId2" name="locoId">
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label text-right">编组数</label>
							<div class="col-md-7 ">
								<input  type="text" id="marshallingNumber2"  name="marshallingNumber" placeholder="请输入编组数" class="form-control input-sm"" />
							</div>
						</div>
						<div class="form-group">
							 <label class="col-md-2 control-label"><span class="text-danger">*</span>编组类型</label>
							<div class="col-md-10">
								<label class="label-radio inline">
									<input type="radio" id="marshallingType1" name="marshallingType" value="1" required="required" 
									  data-parsley-errors-container="#roleError" checked>
									<span class="custom-radio"></span>
									长编
								</label>
								<label class="label-radio inline">
									<input type="radio" id="marshallingType2" name="marshallingType" value="2" required="required" data-parsley-errors-container="#roleError" >
									<span class="custom-radio"></span>
									短遍
								</label>
								<label class="label-radio inline">
									<input type="radio" id="marshallingType3" name="marshallingType" value="3" required="required" data-parsley-errors-container="#roleError" >
									<span class="custom-radio"></span>
									混合编
								</label>
							</div>
							<span id="roleError"></span>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label text-right">定编数</label>
							<div class="col-md-7 ">
								<input  type="text" id="setNum2"  name="setNum" placeholder="请输入定编数" class="form-control input-sm"" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-md-2 control-label"><span style="color: red">*</span>担当局</label>
							<div class="col-md-3">
								<select name="bearBureau" id="bearBureau2" class="form-control chzn-select"  data-placeholder="请选择担当局">
									
									<#if departmentList?? && departmentList?size gt 0>
										<#list departmentList as department>
											<option value="${(department.id)!}">${(department.departName)!}</option>
										</#list>
									</#if>
								</select>
							</div>
						</div>
						<div class="form-group text-center">
							<button type="button" class="btn btn-success" onclick="editLoco()"> 保存  </button>
							<button type="button" class="btn btn-default" data-dismiss="modal"> 关闭  </button>
						</div>
					</form>
			    </div>
		  	</div>
		</div>
	</div>
	
	
	<script language="javascript">
	
	function addLoco(){
		var locoName1 = $("#locoName1").val();
		var marshallingNumber1 = $("#marshallingNumber1").val();
		var setNum1 = $("#setNum1").val();
		var bearBureau = $('#bearBureau1 option:selected') .val();
		
		if($.trim(locoName1) == "" ){
			alertify.alert("请输入机车名称！");
			return false;
		}
		if($.trim(marshallingNumber1) == "" ){
			alertify.alert("请输入编组数！");
			return false;
		}
		if($.trim(setNum1) == "" ){
			alertify.alert("请输入定编数！");
			return false;
		}
		if($.trim(bearBureau) == "" ){
			alertify.alert("请选择担当局！");
			return false;
		}
		
		$.ajax({
			cache: true,
			type: "POST",
			url:"/locomotive/save",
			data:$('#addLocoForm').serialize(),
			async: false,
			error: function(request) {
				alertify.alert("错误：服务器异常！");
			},
			success: function(data) {
				if(data.success){
					location.href="/locomotive/list";
				}else{
					alertify.alert("错误:" + data.message);
				}
			}
		});
	}
	
	function getLoco(locoId){
		if(locoId != null && locoId != ""){
			$.ajax({
				cache: true,
				type: "POST",
				url:"/locomotive/getLoco",
				data:"locoId=" + locoId,
				async: false,
				error: function(request) {
					alertify.alert("错误：服务器异常！");
				},
				success: function(data) {
					if(data.success){
						var loco = data.locomotive;
						$("#locoId2").val(loco.locoId);
						$("#locoName2").val(loco.locoName);
						$("#marshallingNumber2").val(loco.marshallingNumber);
						$("#setNum2").val(loco.setNum);
						
						if(loco.marshallingType == 1){
							$("#marshallingType1").prop("checked",true);
							$("#marshallingType2").prop("checked",false);
							$("#marshallingType3").prop("checked",false);
						}else if(loco.marshallingType == 2){
							$("#marshallingType1").prop("checked",false);
							$("#marshallingType2").prop("checked",true);
							$("#marshallingType3").prop("checked",false);
						}else{
							$("#marshallingType1").prop("checked",false);
							$("#marshallingType2").prop("checked",false);
							$("#marshallingType3").prop("checked",true);
						}
						
						$("#bearBureau2").val(loco.bearBureau);
						
					}else{
						alertify.alert("错误:" + data.message);
					}
				}
			});
		}
	}
	
	function editLoco(){
		var locoName2 = $("#locoName2").val();
		var marshallingNumber2 = $("#marshallingNumber2").val();
		var setNum2 = $("#setNum2").val();
		
		if($.trim(locoName2) == "" ){
			alertify.alert("请输入机车名称！");
			return false;
		}
		if($.trim(marshallingNumber2) == "" ){
			alertify.alert("请输入编组数！");
			return false;
		}
		if($.trim(setNum2) == "" ){
			alertify.alert("请输入定编数！");
			return false;
		}
		
		$.ajax({
			cache: true,
			type: "POST",
			url:"/locomotive/save",
			data:$('#editLocoForm').serialize(),
			async: false,
			error: function(request) {
				alertify.alert("错误：服务器异常！");
			},
			success: function(data) {
				if(data.success){
					location.href="/locomotive/list";
				}else{
					alertify.alert("错误:" + data.message);
				}
			}
		});
	
	
	
	
	
	
	
	
	}
	
	</script>
</@common.commomFrame>